package com.example.myapplication.datastore

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Slider
import androidx.compose.material3.SliderDefaults
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun CounterCompose(padding: PaddingValues, viewModel: CounterViewModel) {
    //从viewModel中读取counter数据
    var appConfig = viewModel.appConfig.collectAsState().value

//    var fontSizeScale by rememberSaveable { mutableStateOf(1F) }
//    var darkMode by rememberSaveable { mutableStateOf(false) }

    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
        modifier = Modifier
            .padding(padding)
            .background(color = if (appConfig.darkMode) Color.Black else Color.White)
            .fillMaxWidth()
    ) {
        Text(
            text = "当前计数：${appConfig.counter}",
            color = if (appConfig.darkMode) Color.White else Color.Black,
            fontSize = 24.sp * appConfig.fontSizeScale
        )

        Row {
            Button(onClick = { viewModel.increment() }, modifier = Modifier.padding(8.dp)) {
                Text(
                   text = "增加",
                    color = if (appConfig.darkMode) Color.White else Color.Black,
                )
            }
            Button(onClick = { viewModel.decrement() }, modifier = Modifier.padding(8.dp)) {
                Text(
                    text = "减少",
                    color = if (appConfig.darkMode) Color.White else Color.Black,
                )
            }
        }

        Slider(
            value = appConfig.fontSizeScale,
            onValueChange = { prog ->
                viewModel.updateFontSize(prog)
            },
            valueRange = 0.8f..1.5f,
            colors = SliderDefaults.colors(
                activeTrackColor = Color(0xFF4D5C8E),
                inactiveTrackColor = Color(0xFFDEE2F7)
            ),
            modifier = Modifier
                .padding(start = 10.dp, end = 20.dp)
                .fillMaxWidth()
        )

        Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
            Text(
                text = "夜间模式",
                fontSize = 16.sp,
                color = if (appConfig.darkMode) Color.White else Color.Black,
            )
            Switch(checked = appConfig.darkMode, onCheckedChange = {flag->
                viewModel.updateDarkMode(flag)
            })
        }
    }
}